﻿using System;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace CI
{
    /// <summary>
    /// Screen saver handling class
    /// </summary>
    public static class ScreenSaver
    {

        private const int SPI_SETSCREENSAVEACTIVE = 17;
        private static readonly SimEnv SimEnv = SimEnv.Instance;

        /// <summary>
        /// System Paramter
        /// </summary>
        /// <param name="uiAction"></param>
        /// <param name="uiParam"></param>
        /// <param name="pvParam"></param>
        /// <param name="fWinIni"></param>
        /// <returns></returns>
        [DllImport("user32", CharSet = CharSet.Auto)]
        unsafe public static extern short SystemParametersInfo(int uiAction, int uiParam, int* pvParam, int fWinIni);

        /// <summary>
        /// Set the screen saver to us
        /// </summary>
        public static void SetScreenSaver()
        {
            // set the screen saver
            // info from http://bytes.com/groups/net-c/263953-setting-up-screensaver-via-csharp-c-application
            try
            {
                RegistryKey oKey = Registry.CurrentUser.OpenSubKey("Control Panel", true);
                oKey = oKey.OpenSubKey("Desktop", true);
                oKey.SetValue("SCRNSAVE.EXE", System.Environment.SystemDirectory + @"\Meso.scr");
                oKey.SetValue("ScreenSaveActive", "1");
                oKey.SetValue("ScreenSaveTimeOut", "60");

                unsafe
                {
                    int nX = 1;
                    SystemParametersInfo(
                        SPI_SETSCREENSAVEACTIVE,
                        0,
                        &nX,
                        0
                        );
                }
            }
            catch (Exception exc)
            {
                System.Windows.MessageBox.Show(exc.ToString());
            }

            SimEnv.TraceLogger.LogDebug("Screen saver has been set to Mesopotamia");
        }

        /// <summary>
        /// Are we the current screen saver
        /// </summary>
        public static bool AreWeCurrentScreenSaver()
        {
            bool _result = false;

            try
            {
                RegistryKey oKey = Registry.CurrentUser.OpenSubKey("Control Panel", true);
                oKey = oKey.OpenSubKey("Desktop", true);
                string screenSaver = (string)oKey.GetValue("SCRNSAVE.EXE");
                if (string.IsNullOrEmpty(screenSaver))
                {
                    SimEnv.TraceLogger.LogDebug("Screen Saver is not set ");
                    return false;
                }
                string ourScreenSaver = System.Environment.SystemDirectory + @"\MesScrSv.scr";
                SimEnv.TraceLogger.LogDebug("Current Screen Saver Mesopotamia : " + screenSaver + ":" + screenSaver.Length);
                SimEnv.TraceLogger.LogDebug("Our Screen Saver Mesopotamia : " + ourScreenSaver + ":" + ourScreenSaver.Length);
                if (screenSaver.Equals(ourScreenSaver))
                    _result = true;
            }
            catch (Exception exc)
            {
                SimEnv.TraceLogger.LogException("Exception Checking Screen Saver", exc);
                System.Windows.MessageBox.Show(exc.ToString());
            }
            SimEnv.TraceLogger.LogDebug("Is Screen saver set to Mesopotamia : " + _result);
            return _result;

        }
    }
}


